package com.junbao.message.ui.testservlet;

import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.junbao.commom.sign.md5.MD5Utils;
import com.junbao.commom.util.RandomUtil;
import com.junbao.message.infrastructure.prop.MessageConfig;
import com.junbao.message.infrastructure.result.Result;
import com.junbao.message.ui.web.BaseServlet;

/**
 * 消息ack确认
 * 被动方应用处理完业务进行消息确认
 * @author yilin
 *
 */
@SuppressWarnings("all")
@WebServlet("/test/handle")
public class MessageHandleTestServlet extends BaseServlet{

	private static final Logger LOGGER = LoggerFactory.getLogger(MessageHandleTestServlet.class);
	
	private static final long serialVersionUID = 1L;
	
	private static final MessageConfig CONFIG = new MessageConfig();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Result result = new Result(Result.SUCCESS.getResult_code(), Result.SUCCESS.getResult_msg());
			if (System.currentTimeMillis() % 2 != 0) {
				result.setResult_code("9999");
				result.setResult_msg("失败返回信息测试！");
			}else {
				Thread.sleep(new Random().nextInt(20));
				if (System.currentTimeMillis() % 2 !=0) {
					result.putData("ack_flag", "0");
				}else {
					result.putData("ack_flag", "1");
				}
			}
			String randomStr = RandomUtil.getUuidString();
			result.putData("sign_parm", randomStr);
			result.putData("sign", MD5Utils.signBuildByStr(randomStr.substring(5, 15), CONFIG.get("message.md5.key")));
			writer(response, result);
			return;
		} catch (Exception e) {
			Result resultData = new Result("9999", e.getMessage());
			writer(response, resultData);
			return;
		}
	}
}
